import matplotlib.pyplot as plt
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

# 1.读取数据
data = pd.read_csv("customers.csv")
# print(data.head())


x = data.iloc[:, [3,4]]  #

# score = []
# score_list = []
# for i in range(2,20):
#     model = KMeans(n_clusters=i)
#     model.fit(x)
#     y_pred = model.predict(x)
#     score.append(model.inertia_)
#     score_list.append(silhouette_score(x,y_pred))

# plt.figure(figsize=(20,10),dpi=80)
# t1 = plt.subplot(1,2,1)
# t1.plot(range(2,20),score,marker='o')
# t2 = plt.subplot(1,2,2)
# t2.plot(range(2,20),score_list,marker='o')
# plt.show()

model = KMeans(n_clusters=5)
model.fit(x)
y_pred = model.predict(x)


plt.scatter(x.values[y_pred == 0,0],x.values[y_pred == 0,1], s=100,c='red', label = 'Standard')
plt.scatter(x.values[y_pred == 1,0],x.values[y_pred == 1,1], s=100,c='blue', label = 'Traditional')
plt.scatter(x.values[y_pred == 2,0],x.values[y_pred == 2,1], s=100,c='green', label = 'Normal')
plt.scatter(x.values[y_pred == 3,0],x.values[y_pred == 3,1], s=100,c='pink', label = 'Youth')
plt.scatter(x.values[y_pred == 4,0],x.values[y_pred == 4,1], s=100,c='orange', label = 'TA')
plt.scatter(model.cluster_centers_[:,0],model.cluster_centers_[:,1],s=300,c='black',label='Centroids')

# plt.title()
# plt.xlabel()
# plt.ylabel()
plt.legend()
plt.show()